﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
using System.Data;
using Comm;
using BusinessFacade;
using Entities;

namespace MedicareNoteManagerWeb.Web
{
    public partial class NoteplanAdd : BasePage
    {
        ListBox listbox2 = new ListBox();//存储人员分组信息
        public static int Consume = 0;//消费
        public static int Tincome = 0;//收入
        public static string ID = "";//页面传的ID
        public static string num = "";
        Busi_nm_noteplan model1 = new Busi_nm_noteplan();
        /// <summary>
        /// 加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {

                model1 = (Busi_nm_noteplan)Session["NoteplanModel"];
                BindDrop();
                try
                {
                    string num = Session["Length"].ToString();
                    string[] strval = num.Split(',');
                    foreach (var item in strval)
                    {
                        ListBox1.Items.Add(item);
                    }
                }
                catch (Exception ex)
                {
                    lbl_Message.Text = ex.Message;
                    lbl_Message.Text = "";
                }
                try
                {
                    ID = "";
                    ID = Request.QueryString["ID"].ToString();
                }
                catch (Exception ex)
                {
                    lbl_Message.Text = ex.Message;
                    lbl_Message.Text = "";
                }
                if (ID != "")
                {
                    FillValue();
                }
                try
                {
                    Busi_nm_noteplan plan = new Busi_nm_noteplan();
                    DataTable dt = plan.GetMaxValue().Tables[0];
                    num = dt.Rows[0]["NUM"].ToString();
                }
                catch (Exception ex)
                {
                    lbl_Message.Text = ex.Message;
                    lbl_Message.Text = "";
                }
                try
                {
                    Busi_nm_noteplan plan = new Busi_nm_noteplan();
                    nm_noteplan_Model model = plan.GetListModel(Convert.ToInt32(ID));
                    DataTable dt = plan.GetsafeID(model.PlanId).Tables[0];
                    ListBox1.Items.Clear();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ListBox1.Items.Add(dt.Rows[i]["Name"].ToString());
                    }
                }
                catch (Exception ex)
                {
                    lbl_Message.Text = ex.Message;
                    lbl_Message.Text = "";
                }
            }
        }


        /// <summary>
        /// 修改填充数据
        /// </summary>
        public void FillValue()
        {
            Button4.Text = "修改";
            Button1.Visible = true;
            Busi_nm_noteplan u = new Busi_nm_noteplan();
            nm_noteplan_Model model = u.GetListModel(Convert.ToInt32(ID));
            notemouble.SelectedValue = model.moubleID.ToString();
            txt_Content.Text = model.NoteContent.ToString();
            txt_Explain.Text = model.PlanExplain.ToString();
            txt_planname.Text = model.PlanName.ToString();
            txtSendTime.Text = Convert.ToDateTime(model.SendTime).ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 绑定模板
        /// </summary>
        public void BindDrop()
        {
            FillDropDownList("GetDrop1", "ID", "MoubleName", "请选择模板", this.notemouble);
        }

        /// <summary>
        /// 生成短信
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Date"></param>
        /// <param name="Consume"></param>
        /// <param name="Sum"></param>
        /// <returns></returns>
        public string CreateNote(string Name, DateTime Date, Double Consume, Double Sum)
        {
            string str = "";
            str += "尊敬的" + Name + ":" + "\r\n";
            str += "      " + "你在" + Date.Year + "年";
            str += Date.Month + "月";
            str += "总消费" + Consume + "元，";
            str += "总收入" + Sum + "元。";
            return str;
        }

        /// <summary>
        /// 验证值是否正确
        /// </summary>
        /// <returns></returns>
        public string CheckData()
        {
            string strErr = "";
            if (string.IsNullOrEmpty(txt_planname.Text.Trim()))
            {
                strErr += "计划名称不为空";
                return strErr;
            }

            if (notemouble.SelectedValue == "0")
            {
                strErr += "请选择模板";
                return strErr;
            }

            if (string.IsNullOrEmpty(txt_Content.Text.Trim()))
            {
                strErr += "短信内容不为空";
                return strErr;
            }

            if (string.IsNullOrEmpty(txt_Explain.Text.Trim()))
            {
                strErr += "计划说明不为空";
                return strErr;
            }

            if (txtSendTime.Text == "")
            {
                strErr += "请选择发送时间";
                return strErr;
            }

            if (ListBox1.Items.Count < 0)
            {
                strErr += "人员分组不为空";
                return strErr;
            }

            return strErr;
        }

        /// <summary>
        /// 清空值
        /// </summary>
        protected void ClearValue()
        {
            txt_planname.Text = "";
            notemouble.SelectedIndex = 0;
            txt_Content.Text = "";
            txt_Explain.Text = "";
            txtSendTime.Text = "";
            ListBox1.Items.Clear();
        }




        //添加
        protected void Button4_Click(object sender, EventArgs e)
        {
            model1 = (Busi_nm_noteplan)Session["NoteplanModel"];
            string strErr = CheckData();
            if (string.IsNullOrEmpty(strErr))
            {
                if (ID == "")
                {
                    //接受值
                    DataSet dset = (DataSet)Session["table"];
                    DataTable dt = dset.Tables[0];
                    su_mapped_Model model11 = new su_mapped_Model();
                    sys_user_Model user = new sys_user_Model();
                    nm_noteplan_Model model = new nm_noteplan_Model();
                    DataSet dsGeTincome = new Busi_nm_noteplan().GeTincome(Convert.ToInt32(dt.Rows[0]["safeID"].ToString()));
                    DataSet dsGetConsume = new Busi_nm_noteplan().GetConsume(Convert.ToInt32(dt.Rows[0]["safeID"].ToString()));
                    Consume = Convert.ToInt32(dsGetConsume.Tables[0].Rows[0]["Consume"].ToString());
                    Tincome = Convert.ToInt32(dsGeTincome.Tables[0].Rows[0]["paymentbase"].ToString());
                    model.moubleID = int.Parse(notemouble.SelectedValue);
                    model.NoteContent = CreateNote(dt.Rows[0]["safeID"].ToString(), DateTime.Now, Consume, Tincome);
                    model.PlanExplain = txt_Explain.Text;
                    model.PlanName = txt_planname.Text;
                    model.SendTime = Convert.ToDateTime(txtSendTime.Text);
                    model.AuditingPerson = user.LoginName;
                    model.AuditingState = 0;
                    model.AuditingSuggestion = "无";
                    model.AuditingTime = DateTime.Now;
                    model.CreatePersonID = user.UserID;
                    model.CreateTime = DateTime.Now;
                    model.PersonGroup = ListBox1.Text;
                    Busi_nm_noteplan users = new Busi_nm_noteplan();
                    DataTable ds = users.GetList(model.PlanName, Convert.ToInt32(-1));
                    if (!(ds == null || ds.Rows.Count == 0))
                    {
                        MessageBox.ShowDialog(Page, "数据已存在请修改！", "error");
                        return;
                    }
                    if (new Busi_nm_noteplan().Add(model, model11))
                    {
                        MessageBox.ShowDialog(this.Page, "添加成功！", "success");
                        lbl_Message.Text = "添加成功！";
                        ClearValue();
                    }
                    else
                    {
                        MessageBox.ShowDialog(this.Page, "添加失败！", "error");
                    }

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (i == 0)
                        {
                            txt_Content.Text = CreateNote(dt.Rows[i]["safeID"].ToString(), DateTime.Now, Consume, Tincome);

                        }
                        DataSet GeTincome = new Busi_nm_noteplan().GeTincome(Convert.ToInt32(dt.Rows[i]["safeID"].ToString()));
                        DataSet GetConsume = new Busi_nm_noteplan().GetConsume(Convert.ToInt32(dt.Rows[i]["safeID"].ToString()));
                        model11.Name = dt.Rows[i]["safeID"].ToString();
                        model11.MappedChar = CreateNote(model11.Name, DateTime.Now, Convert.ToInt32(GetConsume.Tables[0].Rows[0]["Consume"].ToString()), Convert.ToInt32(GeTincome.Tables[0].Rows[0]["paymentbase"].ToString()));
                        model11.TelPhone = dt.Rows[i]["TelPhone"].ToString();
                        Busi_nm_noteplan plan = new Busi_nm_noteplan();
                        model11.PlanID = Convert.ToInt32(num);
                        if (new Busi_nm_noteplan().su_mapped_ADD(model11))
                        {
                            MessageBox.ShowDialog(this.Page, "添加成功！", "success");
                            lbl_Message.Text = "添加成功！";
                            ClearValue();
                        }
                        else
                        {
                            MessageBox.ShowDialog(this.Page, "添加失败！", "error");
                        }
                    }
                }
                else
                {
                    //修改
                    try
                    {
                        nm_noteplan_Model modell = new nm_noteplan_Model();
                        modell.moubleID = int.Parse(notemouble.SelectedValue);
                        modell.NoteContent = txt_Content.Text;
                        modell.PlanExplain = txt_Explain.Text;
                        modell.PlanName = txt_planname.Text;
                        modell.SendTime = Convert.ToDateTime(txtSendTime.Text);
                        Busi_nm_noteplan users = new Busi_nm_noteplan();
                        DataTable ds = users.GetList(modell.PlanName, Convert.ToInt32(modell.moubleID));
                        if (!(ds == null || ds.Rows.Count == 0))
                        {
                            MessageBox.ShowDialog(Page, "数据已存在请修改！", "error");
                            return;
                        }
                        else
                        {
                            new Busi_nm_noteplan().Update(modell);
                            MessageBox.ShowDialog(Page, "修改成功！", "success");
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.ShowDialog(Page, "修改失败！" + ex.Message, "error");
                    }
                }
            }
            else
            {
                lbl_Message.Text = strErr;
            }
        }


        /// <summary>
        /// 移除人员分组
        /// </summary>
        /// <param name="ListbrMan_life"></param>
        /// <param name="ListbrMan_right"></param>
        private void Memove(ListBox ListBox1, ListBox ListBox2)
        {
            int i = 0;

            while (i < ListBox1.Items.Count)
            {
                if (ListBox1.Items[i].Selected)
                {
                    ListBox2.Items.Add(ListBox1.Items[i]);
                    ListBox1.Items.Remove(ListBox1.Items[i]);
                }
                else
                {
                    i += 1;
                }
            }
        }



        /// <summary>
        /// 移除人员分组
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void Remove_Click(object sender, EventArgs e)
        {
            Memove(ListBox1, listbox2);
            try
            {
                for (int i = 0; i <= listbox2.Items.Count; i++)
                {
                    Busi_nm_noteplan.Delete_su_mapped(listbox2.Items[i].Text, Convert.ToInt32(ID));
                }
                MessageBox.ShowDialog(Page, "删除成功！", "success");
            }
            catch (Exception ex)
            {
                MessageBox.ShowDialog(Page, "删除失败！" + ex.Message, "error");
            }
        }
    }
}